/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hitechbangla.fctu.base;

import fctu.simulator.HTB_MAIN;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.Serializable;

/**
 *
 * @author Asus
 */
public class MeasureHelper implements Serializable {
    enum PointSet{
        FIRST_POINT,
        SECOND_POINT,
        NONE
    };
    
    PointSet lastPointSet;
    //Point firstPoint, secondPoint, temporaryPoint;
    double firstPointX, firstPointY, secondPointX, secondPointY, temporaryPointX, temporaryPointY;
    
    public MeasureHelper(){
        
        lastPointSet = PointSet.NONE;
        //firstPoint = new Point();
//        secondPoint= new Point();
//        temporaryPoint = new Point(); 
        
       // firstPoint.
    }//end of constructor
    
    //this function is called when user has clicked a position on screen
    //public void clickedPosition(Point point)
    //public void clickedPosition(Point point)
    //x and y will be passed in nautical miles
    public void clickedPosition(double x, double y)
    {
        switch(lastPointSet)
        {
            case NONE:
                //finalPoint = point;
                //firstPoint = point;
                firstPointX = x;
                firstPointY = y;
                lastPointSet = PointSet.FIRST_POINT;
                //System.out.println("from measurehelper: none, x:" + firstPointX);		
                break;
            case FIRST_POINT:
                //secondPoint = point;
                secondPointX = x;
                secondPointY = y;
                lastPointSet = PointSet.SECOND_POINT;
                //System.out.println("from measurehelper: first " );
                break;
            case SECOND_POINT:
                lastPointSet = PointSet.FIRST_POINT;
                //firstPoint = point;
                firstPointX = x;
                firstPointY = y;
                break; 
            default:
                break;
        }//end of switch case
    }//jend of click position
    
    
    //whenever mouse cursor is moved this function should be called
    //public void updateCursorPosition(Point point){
    public void updateCursorPosition(float x, float y) {
        //temporaryPoint = point;
        temporaryPointX = x;
        temporaryPointY = y;     
    }//end of update cursor postion
    
    public void draw(Graphics g)
    {
        ////System.out.println("from utility draw");
        
        Color c = g.getColor();
        
        //g.setColor(Color.orange);
        g.setColor(Color.BLACK);
        
        //Point finalPoint = new Point();  
        double finalPointX = 0, finalPointY = 0;
        
         switch(lastPointSet)
         {
               case NONE:
                //firstPoint = point;
                //lastPointSet = PointSet.FIRST_POINT;
                   //finalPoint = firstPoint;
                return;
                //break;
               // break;
            case FIRST_POINT:
                //secondPoint = point;
                //lastPointSet = PointSet.SECOND_POINT;
                //finalPoint = temporaryPoint;
                finalPointX = temporaryPointX;
                finalPointY = temporaryPointY;
                break;
            case SECOND_POINT:
               // lastPointSet = PointSet.FIRST_POINT;
                //firstPoint = point;
                //finalPoint = secondPoint;
                finalPointX = secondPointX;
                finalPointY = secondPointY;
                break;         
        }//end of switch case
         
        //g.drawLine(0, 0, 100, 100);
         //g.drawLine(firstPoint.x, firstPoint.y, finalPoint.x, finalPoint.y);
         //drawing cross hair in lower portion
         int crossHairSize = 5;
	 
         g.drawLine((int)(firstPointX * HTB_MAIN.one_px_equivalent_naut - crossHairSize), 
                 (int)(firstPointY * HTB_MAIN.one_px_equivalent_naut), 
                 (int)(firstPointX * HTB_MAIN.one_px_equivalent_naut + crossHairSize),
                 (int) (firstPointY * HTB_MAIN.one_px_equivalent_naut));
         g.drawLine((int) (firstPointX * HTB_MAIN.one_px_equivalent_naut),
                (int) (firstPointY * HTB_MAIN.one_px_equivalent_naut - crossHairSize),
                (int) (firstPointX * HTB_MAIN.one_px_equivalent_naut ),
                (int) (firstPointY * HTB_MAIN.one_px_equivalent_naut + crossHairSize));
         //drawig cross hair uppore portion
         g.drawLine((int) (finalPointX * HTB_MAIN.one_px_equivalent_naut - crossHairSize),
                (int) (finalPointY * HTB_MAIN.one_px_equivalent_naut),
                (int) (finalPointX * HTB_MAIN.one_px_equivalent_naut + crossHairSize),
                (int) (finalPointY * HTB_MAIN.one_px_equivalent_naut));
         g.drawLine((int) (finalPointX * HTB_MAIN.one_px_equivalent_naut),
                (int) (finalPointY * HTB_MAIN.one_px_equivalent_naut - crossHairSize),
                (int) (finalPointX * HTB_MAIN.one_px_equivalent_naut),
                (int) (finalPointY * HTB_MAIN.one_px_equivalent_naut + crossHairSize));
        
         g.drawLine((int)(firstPointX * HTB_MAIN.one_px_equivalent_naut), (int)(firstPointY * HTB_MAIN.one_px_equivalent_naut),
                 (int)(finalPointX * HTB_MAIN.one_px_equivalent_naut), (int)(finalPointY * HTB_MAIN.one_px_equivalent_naut));
         
	 
         
        double angle, distance;
		//angle = Utility::true_north_to_cartesian_conversion((1/ DEGREE_TO_RADIAN) * Utility::findAngle((final_point->get_x() - first_point->get_x()), (final_point->get_y() - first_point->get_y())));
		//angle = Utility::findAngle((final_point->get_x() - first_point->get_x()), (final_point->get_y() - first_point->get_y()));
        double xd, yd;
        //xd = finalPoint.x - firstPoint.x;
        xd = finalPointX - firstPointX;
        //yd = finalPoint.y - firstPoint.y;
        yd = finalPointY - firstPointY;
        
        angle = (double) (Math.atan2(yd, xd) * 180 /3.1416);
       // angle = cartesianToTrueNorth(angle) ;    
        angle = cartesianToTrueNorth(angle) ;
        //distance = distance( finalPointX, finalPointY, firstPointX, firstPointY) * HTB_MAIN.one_nm_equivalent_pixel;               
        distance = distance( finalPointX, finalPointY, firstPointX, firstPointY);
     
       
         //Measure helper HTB main
        // HTB_MAIN.customDrawString(g, "X: " + finalPoint.x + ",  Y: "+finalPoint.y+",  Angle: "+angle, finalPoint);
         //HTB_MAIN.customDrawString(g, "X: " + finalPointX * HTB_MAIN.one_nm_equivalent_pixel+ ",  Y: "+finalPointY * HTB_MAIN.one_nm_equivalent_pixel, finalPoint);
        HTB_MAIN.customDrawString(g, "X: " + finalPointX * HTB_MAIN.one_nm_equivalent_pixel + ",  Y: " + finalPointY * HTB_MAIN.one_nm_equivalent_pixel, 
                new Point((int)(finalPointX * HTB_MAIN.one_px_equivalent_naut), (int) (finalPointY * HTB_MAIN.one_px_equivalent_naut)));
        HTB_MAIN.customDrawString2(g, "Angle: "+angle, 
                new Point((int)(finalPointX * HTB_MAIN.one_px_equivalent_naut), (int) (finalPointY * HTB_MAIN.one_px_equivalent_naut)));
        HTB_MAIN.customDrawString3(g, "Distance: "+distance, 
                new Point((int)(finalPointX * HTB_MAIN.one_px_equivalent_naut), (int) (finalPointY * HTB_MAIN.one_px_equivalent_naut)));
        // HTB_MAIN.pop_matrix((Graphics2D)g);
        g.setColor(c);
        
    }//end draw
    
    
    public double distance(double x1,double y1,double x2,double y2)
    {
        
            return (double) Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
             //return 0;
             
    }//end of distance
    
    
    
     public double cartesianToTrueNorth(double angleCartesian)
    {
                    double temp = 360 - (angleCartesian - 90);
                    if (temp >= 360)
                    {
                            return (temp - 360);
                    }
                    return temp;
    }
     
     
}//end of class


